﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DBLibrary
{
    public class Administrator : Customer
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public Administrator()
            : base() { }

        /// <summary>
        /// Normal constructor
        /// </summary>
        /// <param name="id">ID of the customer</param>
        /// <param name="nickname">nickname of the customer</param>
        /// <param name="password">password of the customer</param>
        public Administrator(int id, string nickname, string password) : base(id, nickname, password) { }

        /// <summary>
        /// Get a specific administrator
        /// </summary>
        /// <param name="id">ID of the wanted admin</param>
        /// <returns>Returns the admin matching the ID, null otherwise</returns>
        public Administrator getAdministrator(int id)
        {
            Administrator result = null;
            try
            {
                // Open the connection to the database
                //
                connection.Open();

                // Build the SQL query
                //
                SqlCommand selectCommand = new SqlCommand("SELECT * FROM Customer WHERE id = " + id + " AND is_admin = 1", connection);

                // Execute query
                //
                SqlDataReader reader = selectCommand.ExecuteReader();

                // Build the administrator
                //
                while (reader.Read())
                {
                    result = new Administrator((int)reader[0], reader[1].ToString(), reader[2].ToString());
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine("An SQL error occured : " + e.Message);
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("An error occured while openning the connection : " + e.Message);
            }
            finally
            {
                connection.Close();
            }

            // Return statement if the customer is not found
            //
            return result;
        }

        /// <summary>
        /// Get all the registered customers
        /// </summary>
        /// <returns>Returns a list of all the registered customers</returns>
        public List<Customer> getAllCustomers()
        {
            List<Customer> result = new List<Customer>();

            try
            {
                // Open the connection to the database
                //
                connection.Open();

                // Build the SQL query
                //
                SqlCommand selectCommand = new SqlCommand("SELECT * FROM Customer", connection);

                // Execute query
                //
                SqlDataReader reader = selectCommand.ExecuteReader();

                // Build the result list
                //
                while (reader.Read())
                {
                    result.Add(new Customer((int)reader[0], reader[1].ToString(), reader[2].ToString()));
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine("An SQL error occured : " + e.Message);
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("An error occured while openning the connection : " + e.Message);
            }
            finally
            {
                connection.Close();
            }

            return result;
        }

        /// <summary>
        /// Add a new administrator into the database
        /// </summary>
        /// <param name="id">ID of the new administrator (primary key)</param>
        /// <param name="nickname">Nickname of the new administrator</param>
        /// <param name="password">Password of the new administrator</param>
        public void addAdmin(string nickname, string password)
        {
            // Add the new admin
            //
            addCustomer(nickname, password, true);
        }

        /// <summary>
        /// Remove a user from the database (including customers and aministrators)
        /// This action means that all albums and pictures of the customers will be deleted
        /// </summary>
        /// <param name="id">ID of the customer to remove</param>
        public void removeUser(int id)
        {
            try
            {
                // Open the connection to the database
                //
                connection.Open();

                // Build the SQL query
                //
                SqlCommand deleteUser = new SqlCommand("DELETE FROM Customer WHERE id = " + id, connection);

                // Execute query
                //
                if (deleteUser.ExecuteNonQuery() == 0) throw new UnfoundCustomerException("Delete failed. The customer may not exists.");
            }
            catch (SqlException e)
            {
                Console.WriteLine("An SQL error occured : " + e.Message);
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("An error occured while openning the connection : " + e.Message);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Grant administrator rights to a simple customer
        /// </summary>
        /// <param name="id">ID of the customer to grant</param>
        public void grantCustomerAdminRights(int id)
        {
            try
            {
                // Open the connection to the database
                //
                connection.Open();

                // Build the SQL query
                //
                SqlCommand updateCommande = new SqlCommand("UPDATE Customer SET is_admin = 1 WHERE id = " + id, connection);

                // Execute query
                //
                if (updateCommande.ExecuteNonQuery() == 0) throw new UpdateException("Update fail. The customer may not exists.");
            }
            catch (SqlException e)
            {
                Console.WriteLine("An SQL error occured : " + e.Message);
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("An error occured while openning the connection : " + e.Message);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Remove an album giving its ID
        /// </summary>
        /// <param name="id">album's ID</param>
        public void removeAlbum(int id)
        {
            try
            {
                // Open the connection to the database
                //
                connection.Open();

                // Build the SQL query
                //
                SqlCommand deleteCommand = new SqlCommand("DELETE FROM Album WHERE id = " + id, connection);

                // Execute query
                //
                if (deleteCommand.ExecuteNonQuery() == 0) throw new UpdateException("Delete fail. The album may not exists.");
            }
            catch (SqlException e)
            {
                Console.WriteLine("An SQL error occured : " + e.Message);
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("An error occured while openning the connection : " + e.Message);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Delete an image giving its ID
        /// </summary>
        /// <param name="id">Image's ID</param>
        public void removeImage(int id)
        {
            try
            {
                // Open the connection to the database
                //
                connection.Open();

                // Build the SQL query
                //
                SqlCommand deleteCommand = new SqlCommand("DELETE FROM Image WHERE id = " + id, connection);

                // Execute query
                //
                if (deleteCommand.ExecuteNonQuery() == 0) throw new UpdateException("Delete fail. The image may not exists.");
            }
            catch (SqlException e)
            {
                Console.WriteLine("An SQL error occured : " + e.Message);
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("An error occured while openning the connection : " + e.Message);
            }
            finally
            {
                connection.Close();
            }
        }
    }
}
